﻿using KZ.Express.ENT;
using KZ.Express.Resource;
using KZ.Utilities;
using System.Data;

namespace KZ.Express.Task
{
    public class CustomerTask : TaskBase
    {
        private new CustomerENT _entity;

        public CustomerTask()
            : base()
        {
            base._entity = _entity = new CustomerENT();
            _entitytype = _entity.GetType();
        }

        public DataTable LoadPartnerGroup()
        {
            DataTable dt = _dalBase.SelectDataTable<PartnerGroupENT>(new SearchCriteria());
            DataRow row = dt.NewRow();
            dt.Rows.InsertAt(row, 0);

            return dt;
        }

        public override int Insert(EntityBase entity)
        {
            base._dalBase.BeginTransaction();

            CustomerENT customer = entity as CustomerENT;
            ValidateData(customer);
            try
            {
                customer.PartnerID = _dalBase.Insert(customer);

                SequenceENT sequence = new SequenceENT();
                sequence.SequenceID = (int)Sequences.Customer;
                sequence.SequenceNext = 1;

                _dalBase.Update(sequence);

                base._dalBase.CommitTransaction();
                return customer.PartnerID;
            }
            catch (DataAccessException ex)
            {
                base._dalBase.RollbackTransaction();
                throw new BusinessLogicException(ex.Message);
            }
            catch
            {
                base._dalBase.RollbackTransaction();
                throw new BusinessLogicException(Message.BLM_ERR_INSERT.ToString());
            }
        }
    }
}